#!/bin/bash

# 环境工程咨询公司管理系统部署脚本

set -e  # 遇到错误时退出

# 部署模式：development 或 production
DEPLOY_MODE=${1:-development}

echo "🚀 开始部署环境工程咨询公司管理系统 (模式: $DEPLOY_MODE)..."

# 检查是否在项目根目录
if [ ! -f "package.json" ] || [ ! -d "backend" ]; then
    echo "❌ 错误: 请在项目根目录运行此脚本"
    exit 1
fi

# 函数：检查命令是否存在
check_command() {
    if ! command -v $1 &> /dev/null; then
        echo "❌ 错误: $1 未安装，请先安装 $1"
        exit 1
    fi
}

# 检查必要的命令
echo "🔍 检查环境依赖..."
check_command "node"
check_command "python3"

if [ "$DEPLOY_MODE" = "development" ]; then
    check_command "conda"
else
    check_command "pm2"
    check_command "nginx"
fi

# 前端构建
echo "📦 构建前端..."
if command -v pnpm &> /dev/null; then
    pnpm install
    pnpm run build
elif command -v npm &> /dev/null; then
    npm install
    npm run build
else
    echo "❌ 错误: 请安装 npm 或 pnpm"
    exit 1
fi

echo "✅ 前端构建完成"

# 后端设置
echo "🐍 设置后端环境..."
cd backend

if [ "$DEPLOY_MODE" = "development" ]; then
    # 开发环境使用conda
    if ! conda env list | grep -q "environmental-api"; then
        echo "📝 创建conda环境..."
        conda create -n environmental-api python=3.11 -y
    fi

    eval "$(conda shell.bash hook)"
    conda activate environmental-api
    pip install -r requirements.txt
else
    # 生产环境使用虚拟环境
    if [ ! -d "venv" ]; then
        echo "📝 创建Python虚拟环境..."
        python3 -m venv venv
    fi

    source venv/bin/activate
    pip install -r requirements.txt

    # 安装生产环境依赖
    pip install gunicorn
fi

# 环境变量设置
if [ "$DEPLOY_MODE" = "production" ]; then
    if [ ! -f ".env" ]; then
        echo "📝 创建生产环境配置..."
        cp .env.example .env
        echo "⚠️  请编辑 backend/.env 文件，设置正确的生产环境配置"
    fi
fi

# 数据库迁移
echo "🗄️ 执行数据库迁移..."
if [ ! -d "alembic" ]; then
    alembic init alembic
fi

# 检查是否有迁移文件
if [ ! "$(ls -A alembic/versions 2>/dev/null)" ]; then
    echo "📝 创建初始迁移..."
    alembic revision --autogenerate -m "Initial migration"
fi

alembic upgrade head

# 初始化数据
echo "📊 初始化示例数据..."
python -m app.init_db

cd ..

if [ "$DEPLOY_MODE" = "production" ]; then
    # 生产环境部署
    echo "🚀 配置生产环境..."

    # 创建日志目录
    mkdir -p logs

    # 更新PM2配置中的路径
    sed -i "s|/path/to/your/project|$(pwd)|g" ecosystem.config.js

    # 启动PM2服务
    pm2 start ecosystem.config.js
    pm2 save
    pm2 startup

    echo "📋 生产环境部署完成！"
    echo ""
    echo "🔧 后续配置步骤:"
    echo "1. 复制Nginx配置:"
    echo "   sudo cp nginx.conf /etc/nginx/sites-available/environmental-consulting"
    echo "   sudo ln -s /etc/nginx/sites-available/environmental-consulting /etc/nginx/sites-enabled/"
    echo "   sudo nginx -t && sudo systemctl reload nginx"
    echo ""
    echo "2. 修改nginx.conf中的域名和路径"
    echo ""
    echo "3. 配置SSL证书 (推荐使用Let's Encrypt):"
    echo "   sudo apt install certbot python3-certbot-nginx"
    echo "   sudo certbot --nginx -d your-domain.com"
    echo ""
    echo "4. 访问地址:"
    echo "   - 网站: http://your-domain.com"
    echo "   - API文档: http://your-domain.com/docs"
    echo ""
    echo "5. 管理命令:"
    echo "   - 查看服务状态: pm2 status"
    echo "   - 重启服务: pm2 restart environmental-api"
    echo "   - 查看日志: pm2 logs environmental-api"
else
    echo "🎉 开发环境部署完成！"
    echo ""
    echo "📋 启动说明:"
    echo "1. 启动后端服务:"
    echo "   cd backend"
    echo "   conda activate environmental-api"
    echo "   python run.py"
    echo ""
    echo "2. 启动前端服务:"
    echo "   npm run dev"
    echo ""
    echo "3. 访问地址:"
    echo "   - 前端: http://localhost:5173"
    echo "   - 后端API: http://localhost:8001"
    echo "   - API文档: http://localhost:8001/docs"
fi

echo ""
echo "4. 默认管理员账户:"
echo "   - 用户名: admin"
echo "   - 密码: admin123"
echo ""
echo "✨ 祝您使用愉快！"